package com.qinge.blog;

import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.api.ProgressCallback;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class GeneratorApplication {
    public void generator() throws Exception {
        List<String> warnings = new ArrayList<String>();
        boolean overwrite = true;
        InputStream in = this.getClass().getResourceAsStream("mybatis-generator.xml");
        ConfigurationParser cp = new ConfigurationParser(warnings);
        Configuration config = cp.parseConfiguration(in);
        DefaultShellCallback callback = new DefaultShellCallback(overwrite);
        MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config,
                callback, warnings);
        myBatisGenerator.generate(new ProgressCallback(){

            @Override
            public void introspectionStarted(int totalTasks) {

            }

            @Override
            public void generationStarted(int totalTasks) {

            }

            @Override
            public void saveStarted(int totalTasks) {

            }

            @Override
            public void startTask(String taskName) {
                System.out.println(taskName);
            }

            @Override
            public void done() {

            }

            @Override
            public void checkCancel() throws InterruptedException {

            }
        });
    }

    public static void main(String[] args) throws Exception {
        try {
            GeneratorApplication generatorApplication = new GeneratorApplication();
            generatorApplication.generator();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
